package com.mzl.lal4cloud.lxw4rf.service.contract.bean;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author J_luo
 */
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ErrorResult implements Serializable {

    private String code;

    private String message;

    private boolean success = true;

    private Object data;

    public static ErrorResult errorBuild() {
        ErrorResult result = new ErrorResult();
        result.setCode("-1");
        result.setMessage("请求失败");
        result.setSuccess(false);
        result.setData(null);
        return result;
    }

    /**
     * 转换json
     *
     * @return
     */
    public static ErrorResult fromJson(String resultStr) {

        if (resultStr.isEmpty()) {
            return ErrorResult.errorBuild();
        }

        ErrorResult result = BeanUtil.toBean(JSONUtil.parseObj(resultStr), ErrorResult.class);
        if (!result.getCode().equals("0")) {
            result.setSuccess(false);
        }
        return result;
    }

}

